#!/bin/sh
# PCP QA Test No. 1132
# Exercise pcp2xlsx.
#
# Copyright (c) 2017-2018 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"
$python -c "import openpyxl" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python openpyxl module not installed"

which unzip >/dev/null 2>&1 || _notrun "unzip not installed"
which pcp2xlsx >/dev/null 2>&1 || _notrun "pcp2xlsx not installed"

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
$sudo rm -rf $tmp $tmp.* $seq.full
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_unzip_filter()
{
    LC_ALL=POSIX sort | \
    grep -v sharedStrings | \
    sed \
       -e "s|Archive:.*||g" \
       -e "s|[ ]*$||g" \
       -e 's/^\([ 	]*\)extracting:/\1 inflating:/' \
    #end
}

_xlsx_filter()
{
    sed \
       -e 's|>.*</dcterms:created>|>|g' \
       -e 's|>.*</dcterms:modified>|>|g' \
    < $1 > $1.f
    mv $1.f $1
}

_path_filter()
{
    sed \
       -e 's|<c r="B2" s="1" t="inlineStr"><is><t>.*archives/rep</t>|<c r="B2" s="1" t="inlineStr"><is><t>archives/rep</t>|g' \
    < $1 > $1.f
    mv $1.f $1
}

A="$here/archives/rep"

# real QA test starts here
echo "--- running pcp2xlsx"
pcp2xlsx -a $A -H -I -z -b MB -P 2 -F $tmp.outfile ""
mkdir -p $tmp.dir/ref $tmp.dir/run
cd $tmp.dir/ref
echo "--- unzipping reference output"
unzip $here/sheet/pcp2xlsx.qa.1132.out | _unzip_filter
_xlsx_filter docProps/core.xml
_path_filter xl/worksheets/sheet1.xml
cd $tmp.dir/run
echo "--- unzipping generated output"
unzip $tmp.outfile | _unzip_filter
_xlsx_filter docProps/core.xml
_path_filter xl/worksheets/sheet1.xml
cd $tmp.dir

# openpyxl produced XML changes across versions to we do just a basic
# sanity check here that the expected data are present in the sheet file
fail=0
echo checking output
check_strings="/usr/lib/systemd/systemd mem.util.used kernel.all.sysfork"
for str in $(echo $check_strings | tr ' ' '\n'); do
	grep -r $str . > /dev/null 2>&1
	[ $? -ne 0 ] && fail=1
	grep -r $str . > /dev/null 2>&1
	[ $? -ne 0 ] && fail=1
done
if [ $fail -eq 1 ]; then
	echo expected data not found in the sheet file
else
	echo output ok
fi

cd $here
rm -f $tmp.outfile

# success, all done
status=0
exit
